package com.trkj.service.wdm;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.wdm.WorkSheetDAO;
import com.trkj.pojo.Worksheet;
import com.trkj.service.ServiceException;

@Service
@Transactional(rollbackFor=Exception.class)
public class WorkSheetService {
	@Autowired
	private WorkSheetDAO worksheetdao;
	
	/**
	 * 分页查询所有
	 * @return
	 */
	public List<Worksheet> queryByPage(int page,int rows){
		return  worksheetdao.queryByNaPage(new PageRequest(page-1, rows,Direction.DESC,"workId"));
	}
	/**
	 * 获取总记录条数
	 * @return
	 */
	public long queryByPageCount(){
		return worksheetdao.count();
	}
	
	/**
	 * 新增
	 */
	public void insert(String workName,String workPerson){
		try {
			Worksheet rea = new Worksheet();
			rea.setWorkName(workName);
			rea.setWorkPerson(workPerson);
			worksheetdao.save(rea);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 编辑
	 */
	public void update(Long workId,String workName,String workPerson){
		try {
			Worksheet rea = worksheetdao.findOne(workId);
			rea.setWorkName(workName);
			rea.setWorkPerson(workPerson);
			worksheetdao.save(rea);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
		
	}
	
	
	
	/**
	 * 删除
	 */
	public void delete(Long id){
		try {
			worksheetdao.delete(id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 加载
	 * @return
	 */
	public Worksheet loadReasonsLeaving(Long bptId) {
		return worksheetdao.findOne(bptId);
	}
	/**
	 * 模糊查询
	 * @param text
	 * @param page
	 * @param rows
	 * @return
	 */
	public List<Worksheet> selectForid_name(String text,int page,int rows){
		try {
			return worksheetdao.query_By_idorname("%"+text+"%",new PageRequest(page-1, rows));
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}

	/**
	 * 获取总记录条数
	 * @return
	 */
	public long count(String text){
		return worksheetdao.count("%"+text+"%");
	}

}
